﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using DBUtility;

namespace ATSDAL
{
    public class BaseRepository<T> where T : class
    {
        #region  基类公用函数
        public void Insert(T Entity)
        {
            InsertOrUpdate(Entity, GetInsertsp());
        }

        public void Update(T Entity)
        {
            InsertOrUpdate(Entity, GetUpdatesp());
        }

        private void InsertOrUpdate(T Entity, string sp_name)
        {
            SqlParameter[] Parms = GetParameters();
            SetParameters(Entity, Parms);
            SqlCommand cmd = new SqlCommand();
            foreach (SqlParameter parm in Parms)
                cmd.Parameters.Add(parm);
            try
            {
                ExecuteCommand(cmd, sp_name);
            }
            catch 
            {
                throw;
            }
        }

        protected void ExecuteCommand(SqlCommand cmd, string sp_name)
        {
            using (SqlConnection conn = new SqlConnection(sqlHelper.ConnectionStringLocalTransaction))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = sp_name;
                using (SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { }
            }
        }

        public void Delete<T1>(T1 Id)
        {
            SqlParameter Parm = GetParameter();
            Parm.Value = Id;
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.Add(Parm);
            try
            {
                ExecuteCommand(cmd, GetDeletesp());
            }
            catch
            {
                throw;
            }
        }

        public T ExcuteSqlToGetEntity(string spname, params SqlParameter[] parm)
        {
            T Entity = null;
            using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, spname, parm))
            {
                if (rdr.Read())
                    Entity = GetEntityRecord(rdr);
                else
                    Entity = GetEntityRecord();
            }
            return Entity;
        }

        public IList<T> ExcuteSqlToGetEntities(string spname, params SqlParameter[] parm)
        {
            IList<T> Entities = new List<T>();
            using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, spname, parm))
            {
                while (rdr.Read())
                {
                    T Entity = GetEntityRecord(rdr);
                    Entities.Add(Entity);
                }
            }
            return Entities;
        }

        #endregion





        #region  虚函数由子类重写
        public virtual T GetEntityRecord(SqlDataReader rdr)
        {
            //由子类重写
            return null;
        }

        public virtual T GetEntityRecord()
        {
            //由子类重写
            return null;
        }

        public virtual string GetInsertsp()
        {
            //由子类重写
            return null;
        }

        public virtual string GetUpdatesp()
        {
            //由子类重写
            return null;
        }

        public virtual string GetDeletesp()
        {
            //由子类重写
            return null;
        }

        public virtual SqlParameter GetParameter()
        {
            //由子类重写
            return null;
        }

        public virtual SqlParameter[] GetParameters()
        {
            //由子类重写
            return null;
        }

        public virtual void SetParameters(T Entity, params SqlParameter[] Parms)
        {
            //由子类重写
        }
        #endregion
    }

    public class BaseRepository_DataSet
    {



        #region  基类公用函数


        public DataSet Execute(string spname)
        {
            DataSet set = new DataSet();
            set = sqlHelper.GetDataSetP(spname, sqlHelper.ConnectionStringLocalTransaction);
            return set;
        }



        public DataSet ExcuteDatasetP(string spname, params SqlParameter[] parm)
        {
            DataSet set = new DataSet();
            set = sqlHelper.GetDataSetP(spname, sqlHelper.ConnectionStringLocalTransaction, parm);

            return set;
        }



        public DataSet ExcuteDataset(string spname, string TableName, params SqlParameter[] parm)
        {
            DataSet set = new DataSet();
            set = sqlHelper.GetDataSet(spname, TableName, sqlHelper.ConnectionStringLocalTransaction, parm);

            return set;
        }

        #endregion
    }
}
